package com.handydata.antifraud.ws.dap;

import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.TianChuangInfoReqTO;
import com.handydata.antifraud.to.dap.TianChuangInfoResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.ws.WsInvocationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * 天创生活探针
 *
 */
@Slf4j
@Service("tianChuangInfoWsService")
@Comment("天创")
public class TianChuangInfoWsServiceImpl extends DataPlatformWsService<TianChuangInfoReqTO, TianChuangInfoResTO>
	implements WsInvocationService<TianChuangInfoReqTO, TianChuangInfoResTO> {

	@Override
	public TianChuangInfoResTO query(TianChuangInfoReqTO req)	throws WsInvokeException {
		log.info("天创请求参数 {}", req);
		TianChuangInfoResTO resTO = new TianChuangInfoResTO();
		if (StringUtils.isEmpty(req.getName())) {
			log.error("天创参数校验 {}", "name为空");
			resTO.setResCode("1111");
			resTO.setResMsg("天创接口name参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getIdCard())) {
			log.error("天创参数校验 {}", "idCard为空");
			resTO.setResCode("1111");
			resTO.setResMsg("天创接口idCard参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getPhoneNum())) {
			log.error("天创参数校验 {}", "mobile为空");
			resTO.setResCode("1111");
			resTO.setResMsg("天创接口mobile参数为空");
			return resTO;
		}
		this.dataType = DataSourceCode.TIAN_CHUANG.getCode();
		resTO = executeQuery(req);
		// 行内压测挡板处理
//		try {
//			Thread.sleep(630);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
//		resTO.setScoreTotalOfTianChuang("200");
//		resTO.setResCode("0000");
//		resTO.setResMsg("查询成功");
		return resTO;
	}
	
	@Override
	protected String createQueryXml(TianChuangInfoReqTO req) {
		StringBuilder xml = new StringBuilder();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
        xml.append("<subatm>");
        xml.append("<application>GwBiz.Req</application>");
        xml.append("<version>1.0.0</version>");
        xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
        xml.append("<transCode>300643</transCode>");
        xml.append("<channelId>").append(encryInfo.getChannelId()).append("</channelId>");
        xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
        xml.append("<name>").append(req.getName()).append("</name>");
        xml.append("<cid>").append(req.getIdCard()).append("</cid>");
        xml.append("<phone>").append(req.getPhoneNum()).append("</phone>");
        xml.append("</subatm>");
        req.setQueryXml(xml.toString());
		return xml.toString();
	}
	
	@Override
	protected TianChuangInfoResTO parseResultAndSave(String resultXml,	String orderId, Date createDate) {
		log.info("调用天创生活探针接口返回报文：{}", resultXml);
		TianChuangInfoResTO resTO = new TianChuangInfoResTO();
		try{
			// 解析xml，保存本地DB
			JSONObject jasonObject = JSONObject.parseObject(resultXml);
			if (null != jasonObject) {
				if ("0000".equals(jasonObject.get("resCode"))) {
					JSONObject resData=(JSONObject) jasonObject.get("resData");
		            Double scoreTotal= Double.valueOf(resData.getString("score_total"));
					resTO.setScoreTotalOfTianChuang(String.valueOf(scoreTotal));
				}
				resTO.setResCode((String)jasonObject.get("resCode"));
				resTO.setResMsg((String)jasonObject.get("resMsg"));
				//resTO.setReqId(dataCallLogsReqBO.getId());
			} else {
				resTO.setResCode("1111");
				resTO.setResMsg("天创生活探针接口返回数据异常");
			}
		} catch (Exception e){
			log.info("调用天创生活探针接口返回报文解析异常：{}", e);
			resTO.setResCode("1111");
			resTO.setResMsg("调用天创生活探针接口返回报文解析异常");
		}
		log.info("调用天创生活探针接口返回结果：{}", JSONObject.toJSONString(resTO));
		return resTO;
	}
	
	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, TianChuangInfoReqTO.class);
	}
	
//	public static void main(String[] args) {
//		String data = "{\"resCode\":\"0000\",\"resMsg\":\"查询成功\",\"resData\":{\"score_total\":0.0175,\"net_var27\":-1,\"net_var28\":-1,\"net_var25\":-1,\"net_var26\":-1,\"net_var29\":-1,\"net_var20\":-1,\"tel\":\"13081949186\",\"net_var23\":-1,\"net_var24\":-1,\"net_var21\":-1,\"net_var22\":-1,\"net_var7\":0,\"net_var8\":0,\"net_var5\":0.0566,\"net_var6\":0,\"net_var9\":0,\"net_var16\":0,\"net_var17\":-1,\"net_var14\":0,\"net_var15\":0,\"net_var3\":0.0438,\"net_var4\":0.4469,\"net_var1\":0,\"net_var18\":-1,\"net_var2\":7,\"net_var19\":-1,\"net_var30\":-1,\"net_var12\":0,\"net_var13\":0,\"net_var10\":0,\"net_var11\":0}}";
//		JSONObject jasonObject = JSONObject.parseObject(data);
//		JSONObject resData=(JSONObject) jasonObject.get("resData");
//        Double scoreTotal=  Double.valueOf(resData.getString("score_total"));
//        System.out.println("======" + scoreTotal);
//        
//	}

}
